/**
 * @projectName study-note
 * @package middle
 * @className middle.AddTwoNumbers
 */
package middle;

import node.ListNode;

/**
 * AddTwoNumbers
 * @description 两链表相加
 * @author answer_wx
 * @date 2022/7/7 21:28
 * @version 1.0
 */
public class AddTwoNumbers {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int val = l1.val + l2.val;
        l1.val = val % 10;
        if (l1.next == null && l2.next == null) {
            if (val >= 10) {
                l1.next = new ListNode(1);
            }
            return l1;
        }
        if (l1.next == null) {
            l1.next = new ListNode(0);
        }
        if (l2.next == null) {
            l2.next = new ListNode(0);
        }
        l1 = l1.next;
        if (val >= 10) {
            l1.val += 1;
        }
        l2 = l2.next;
        return addTwoNumbers(l1, l2);
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9, new ListNode(9, new ListNode(9)));
        ListNode l2 = new ListNode(9, new ListNode(9));
        ListNode listNode = addTwoNumbers(l1, l2);
        while (l1 != null) {
            System.out.println(l1.val);
            l1 = l1.next;
        }
    }
}